<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article#
" lang="zh_cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>aspnet6在CentOS下使用GDI时的问题及解决方法 | vitamind3 竹里馆</title>
<link href="../../assets/css/all-nocdn.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/ipython.min.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/nikola_ipython.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="zh_cn" href="../../rss.xml">
<link rel="canonical" href="https://vitamind3.gitee.io/posts/aspnet6zai-centosxia-shi-yong-gdishi-de-wen-ti-ji-jie-jue-fang-fa/">
<!--[if lt IE 9]><script src="../../assets/js/html5.js"></script><![endif]--><meta name="author" content="vitamind3">
<link rel="prev" href="../kai-zhang-da-ji/" title="开张大吉" type="text/html">
<link rel="next" href="../dui-bi-ge-chong-bi-ji-ruan-jian/" title="对比各种笔记软件" type="text/html">
<meta property="og:site_name" content="vitamind3 竹里馆">
<meta property="og:title" content="aspnet6在CentOS下使用GDI时的问题及解决方法">
<meta property="og:url" content="https://vitamind3.gitee.io/posts/aspnet6zai-centosxia-shi-yong-gdishi-de-wen-ti-ji-jie-jue-fang-fa/">
<meta property="og:description" content="使用 apsnet6 开发的网站通过docker部署到 CentOS Linux 以后，登陆即出错，报错如下。
其中使用的是 mcr.microsoft.com/dotnet/aspnet:6.0  。
 异常类型：TypeInitializationException
 异常信息：The type initializer for 'Gdip' threw an exception.
 【堆栈调用">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2021-12-27T22:07:10+08:00">
<meta property="article:tag" content=".net6">
<meta property="article:tag" content="aspnet">
<meta property="article:tag" content="linux">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">跳到主内容</a>

<!-- Menubar -->

<nav class="navbar navbar-expand-md static-top mb-4
navbar-dark
bg-dark
"><div class="container">
<!-- This keeps the margins nice -->
        <a class="navbar-brand" href="../../">

            <span id="blog-title">vitamind3 竹里馆</span>
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#bs-navbar" aria-controls="bs-navbar" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="bs-navbar">
            <ul class="navbar-nav mr-auto">
<li class="nav-item">
<a href="../../pages/introduction" class="nav-link">欢迎</a>
                </li>
<li class="nav-item">
<a href="../../archive.html" class="nav-link">文章归档</a>
                </li>
<li class="nav-item">
<a href="../../categories/" class="nav-link">分类</a>
                </li>
<li class="nav-item">
<a href="../../rss.xml" class="nav-link">RSS 源</a>

                
            </li>
</ul>
<ul class="navbar-nav navbar-right"></ul>
</div>
<!-- /.navbar-collapse -->
    </div>
<!-- /.container -->
</nav><!-- End of Menubar --><div class="container" id="content" role="main">
    <div class="body-content">
        <!--Body content-->
        
        
        
<article class="post-text h-entry hentry postpage" itemscope="itemscope" itemtype="http://schema.org/Article"><header><h1 class="p-name entry-title" itemprop="headline name"><a href="." class="u-url">aspnet6在CentOS下使用GDI时的问题及解决方法</a></h1>

        <div class="metadata">
            <p class="byline author vcard p-author h-card"><span class="byline-name fn p-name" itemprop="author">
                    vitamind3
            </span></p>
            <p class="dateline">
            <a href="." rel="bookmark">
            <time class="published dt-published" datetime="2021-12-27T22:07:10+08:00" itemprop="datePublished" title="2021-12-27 22:07">2021-12-27 22:07</time></a>
            </p>
            

        </div>
        

    </header><div class="e-content entry-content" itemprop="articleBody text">
    <div>
<p>使用 apsnet6 开发的网站通过docker部署到 CentOS Linux 以后，登陆即出错，报错如下。
其中使用的是 <code class="docutils literal">mcr.microsoft.com/dotnet/aspnet:6.0</code>  。</p>
<pre class="code text"><a id="rest_code_61da66e41a354a71be829d7974774563-1" name="rest_code_61da66e41a354a71be829d7974774563-1"></a> 异常类型：TypeInitializationException
<a id="rest_code_61da66e41a354a71be829d7974774563-2" name="rest_code_61da66e41a354a71be829d7974774563-2"></a> 异常信息：The type initializer for 'Gdip' threw an exception.
<a id="rest_code_61da66e41a354a71be829d7974774563-3" name="rest_code_61da66e41a354a71be829d7974774563-3"></a> 【堆栈调用】：
<a id="rest_code_61da66e41a354a71be829d7974774563-4" name="rest_code_61da66e41a354a71be829d7974774563-4"></a>at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, IntPtr scan0, IntPtr&amp; bitmap)
</pre>
<p>原因是登陆页面使用了 <code class="docutils literal">System.Drawing</code> 绘制验证码。
为解决此问题，需要安装 libgdiplus 库及启用gdi。</p>
<!-- TEASER_END -->
<ol class="arabic">
<li><dl>
<dt>安装 libgdiplus</dt>
<dd><pre class="code shell"><a id="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-1" name="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-1"></a>apt update
<a id="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-2" name="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-2"></a>apt install libgdiplus
<a id="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-3" name="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-3"></a>ln -s /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so
<a id="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-4" name="rest_code_1b671fe39111448c9f8ff07b62a8e3ca-4"></a>ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
</pre></dd>
</dl></li>
<li><dl>
<dt>启用 gdi 。在发布目录下找到一个 <code class="docutils literal">.runtimeconfig.json</code> 文件，添加 <code class="docutils literal">EnableUnixSupport</code> 选项。</dt>
<dd><pre class="code json"><a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-1" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-1"></a><span class="p">{</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-2" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-2"></a>    <span class="nt">"runtimeOptions"</span><span class="p">:</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-3" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-3"></a>    <span class="p">{</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-4" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-4"></a>        <span class="nt">"configProperties"</span><span class="p">:</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-5" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-5"></a>        <span class="p">{</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-6" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-6"></a>            <span class="nt">"System.Drawing.EnableUnixSupport"</span><span class="p">:</span> <span class="kc">true</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-7" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-7"></a>        <span class="p">}</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-8" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-8"></a>    <span class="p">}</span>
<a id="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-9" name="rest_code_fc360030132a4eb59ce9c5c814f9a9cf-9"></a><span class="p">}</span>
</pre></dd>
</dl></li>
</ol>
<p>之所以要启用 gdi ，是因为 .net6 的
<a class="reference external" href="https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only">破坏性更新</a> ，
简单说就是从 .net6 开始不再默认启用跨平台的 <code class="docutils literal">System.Drawing.Common</code> 功能。</p>
<p>所以最后的建议是，如果你更新到了.net6，同时仍在使用 GDI 处理图像——例如生成验证码——那建议从现在开始改用其他的跨平台图像库，例如：</p>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/mono/SkiaSharp">SkiaSharp</a> ，从个人感觉上，更推荐 SkiaSharp ，一是Skia本身是大厂出品，二是SkiaSharp比另两个成熟度更高。</p></li>
<li><p><a class="reference external" href="https://github.com/SixLabors/ImageSharp">ImageSharp</a></p></li>
<li><p><a class="reference external" href="https://github.com/dotnet/Microsoft.Maui.Graphics">Microsoft.Maui.Graphics</a></p></li>
</ul>
</div>
    </div>
    <aside class="postpromonav"><nav><ul itemprop="keywords" class="tags">
<li><a class="tag p-category" href="../../categories/net6/" rel="tag">.net6</a></li>
            <li><a class="tag p-category" href="../../categories/aspnet/" rel="tag">aspnet</a></li>
            <li><a class="tag p-category" href="../../categories/linux/" rel="tag">linux</a></li>
        </ul>
<ul class="pager hidden-print">
<li class="previous">
                <a href="../kai-zhang-da-ji/" rel="prev" title="开张大吉">上一篇文章</a>
            </li>
            <li class="next">
                <a href="../dui-bi-ge-chong-bi-ji-ruan-jian/" rel="next" title="对比各种笔记软件">下一篇文章</a>
            </li>
        </ul></nav></aside></article><!--End of body content--><footer id="footer">
            Contents ©         <a href="https://vitamind3.gitee.io/">vitamind3</a> - Powered by         <a href="https://getnikola.com" rel="nofollow">Nikola</a>         
            
            
        </footer>
</div>
</div>


        <script src="../../assets/js/all-nocdn.js"></script><script>
    baguetteBox.run('div#content', {
        ignoreClass: 'islink',
        captions: function(element){var i=element.getElementsByTagName('img')[0];return i===undefined?'':i.alt;}});
    </script><script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?09027755f7478e129aec2cdd3acf9a4c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>
